package com.ssm.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ssm.mapper.CartMapper;
import com.ssm.pojo.cart.AddCart;
import com.ssm.pojo.cart.AddCartResult;
import com.ssm.pojo.cart.Cart;
import com.ssm.pojo.cart.CartData;
import com.ssm.pojo.cart.CartList;
import com.ssm.pojo.cart.RemoveAll;
import com.ssm.pojo.cart.RemoveAllCartResult;
import com.ssm.pojo.cart.RemoveCart;
import com.ssm.pojo.cart.RemoveCartResult;
import com.ssm.pojo.cart.UpdateCart;
import com.ssm.pojo.cart.UpdateCartResult;
import com.ssm.pojo.order.Order;
import com.ssm.pojo.order.OrderList;
import com.ssm.pojo.product.Product;
import com.ssm.service.CartService;



// 自动注册到Spring容器
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    CartMapper cartMapper;

	@Override
	public CartList list(int userId) {
		// TODO Auto-generated method stub
		boolean cartFlag = true;
		List<CartData> dataList = cartMapper.list(userId);
		CartList cart = new CartList();
		List<OrderList> orderList = new ArrayList<OrderList>();
		for(CartData data:dataList) {
			if(cartFlag) {
				cart.setOrderNum(data.getOrderNum());
				cart.setPrice(data.getTotalPrice());
				cart.setUserId(data.getUserId());
				cart.setType(data.getType());
				cartFlag = false;
			}
			OrderList order = new OrderList();
			order.setId(data.getOrderId());
			order.setOrderNum(data.getOrderNum());
			order.setProductId(data.getProductId());
			order.setPrice(data.getProductPrice());
			order.setQuantity(data.getOrderQuantity());
			Product product = new Product();
			product.setId(data.getProductId());
			product.setName(data.getName());
			product.setDiscount(data.getDiscount());
			product.setShopPrice(data.getShopPrice());
			product.setPrice(data.getProductPrice());
			product.setQuantity(data.getQuantity());
			product.setHot(data.getHot());
			product.setDefaultImg(data.getDefaultImg());
			order.setProductList(product);
			orderList.add(order);
			cart.setOrderItemList(orderList);
		}
		return cart;
	}

	@Override
	public RemoveCartResult delete(RemoveCart removeCart) {
		// TODO Auto-generated method stub
		String id = removeCart.getOrderItem().getId();
		Integer result = cartMapper.delete(id);
		RemoveCartResult error = new RemoveCartResult();
		error.setData(removeCart);
		if(result.equals(1)) {
			error.setErrorCode(0);
			error.setErrorMsg("删除成功");
		}else {
			error.setErrorCode(1);
			error.setErrorMsg("删除失败");
		}
		return error;
	}

	@Override
	@Transactional
	public RemoveAllCartResult deleteAll(int userId) {
		// TODO Auto-generated method stub
		Integer resultOrder = cartMapper.deleteAll(userId);
		Integer resultCart = cartMapper.deleteCart(userId);
		Integer result = resultOrder + resultCart;
		RemoveAllCartResult error = new RemoveAllCartResult();
		RemoveAll remove = new RemoveAll();
		remove.setUserId(userId);
		remove.setResult(result);
		if(result > 0) {
			error.setErrorCode(0);
			error.setErrorMsg("删除成功");
			error.setData(remove);
		}else {
			error.setErrorCode(1);
			error.setErrorMsg("删除失败");
			error.setData(remove);
		}
		return error;
	}

	@Override
	@Transactional
	public AddCartResult add(AddCart addCart) {
		// TODO Auto-generated method stub
		Integer resultCart,resultOrder;
		Cart cart = cartMapper.getCart(addCart.getUserId());
		LocalDateTime dateTime = LocalDateTime.now(); // get the current date and time
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
		String str = dateTime.format(formatter)+new Random().nextInt(10);
		if (cart == null) {
			cart = new Cart();
			cart.setOrderNum("O"+str);
			cart.setType(1);
			cart.setUserId(addCart.getUserId());
			resultCart = cartMapper.addCart(cart);
		}
		Order order = addCart.getOrderItem();
		Order getOrder = cartMapper.getOrder(order);
		BigDecimal price = cartMapper.getPrice(order.getProductId());
		if(getOrder == null) {  
			order.setId("I"+str);
			order.setOrderNum(cart.getOrderNum());
			BigDecimal totalPrice = price.multiply(BigDecimal.valueOf(order.getQuantity()));
			order.setPrice(totalPrice);
			resultOrder = cartMapper.add(order);
		}else {
			order.setId(getOrder.getId());
			int number = getOrder.getQuantity() + order.getQuantity();
			order.setQuantity(number);
			BigDecimal totalPrice = price.multiply(BigDecimal.valueOf(number));
			order.setPrice(totalPrice);
			resultOrder = cartMapper.update(order);
		}
		resultCart = cartMapper.updateCartPrice(cart);
		AddCartResult result = new AddCartResult();
		result.setData(addCart);
		if(resultOrder.equals(1) && resultCart.equals(1)) {
			result.setErrorCode(0);
			result.setErrorMsg("添加成功");
		}else {
			result.setErrorCode(1);
			result.setErrorMsg("添加失败");
		}
		return result;
	}

	@Override
	@Transactional
	public UpdateCartResult update(UpdateCart updateCart) {
		// TODO Auto-generated method stub
		List<Order> orderList = updateCart.getOrderItemList();
		for(Order order : orderList) {
			BigDecimal price = cartMapper.getPrice(order.getProductId());
			BigDecimal totalPrice = price.multiply(BigDecimal.valueOf(order.getQuantity()));
			order.setPrice(totalPrice);
			cartMapper.update(order);
		}
		Cart cart = new Cart();
		cart.setUserId(updateCart.getUserId());
		String id = updateCart.getOrderItemList().get(0).getId();
		cart.setOrderNum(cartMapper.getOrderNum(id));
		Integer resultCart = cartMapper.updateCartPrice(cart);
		UpdateCartResult result = new UpdateCartResult();
		result.setData(updateCart.getUserId());
		if(resultCart.equals(1)) {
			result.setErrorCode(0);
			result.setErrorMsg("修改成功");
		}else {
			result.setErrorCode(1);
			result.setErrorMsg("修改失败");
		}
		return result;
	}

}
